<?php
require('config.php');

        $user_id=$_REQUEST['user_id'];
        $wb_order_id=$_REQUEST['wb_order_id'];
        $pay_way=$_REQUEST['pay_way'];
        $time=date('Y-m-d H:i:s');
        $time2=time();
        $sql="select * from `".$oto."_users` where `userId`='".$user_id."' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_help_orders` where `id`='".$wb_order_id."' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$order[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(!isset($order))
		{
			$data[0]['pan']="-1";
			$data[0]['msg']="订单不存在";
		}
		else
		{
			if($pay_way==3)
			{
				if($user_info[0]['userMoney']>=$order[0]['money'])
				{
					$needPay_lin=$order[0]['money'];
					$user_score_lin=$user_info[0]['userScore']+$needPay_lin;
					$user_money_lin=$user_info[0]['userMoney']-$needPay_lin;
					$sql="update ".$oto."_help_orders set orderStatus='1',payType='{$pay_way}',payTime='{$time}',isPay='1' where id='{$wb_order_id}'";
					$result=$db->query($sql);
					// $sql="INSERT INTO ".$oto."_score_record (`userid`,`orderNo`,`score`,`totalscore`,`time`,`IncDec`,`type`) VALUES ('$user_id','$wb_order_id','$needPay_lin','$user_score_lin','$time2','1','1')";
	    //         	$result=$db->query($sql);
	            	if($pay_way==3) $pay_way_lin=3;
	            	if($pay_way==1) $pay_way_lin=1;
	            	if($pay_way==2) $pay_way_lin=2;
	            	$sql="INSERT INTO ".$oto."_money_record (`userid`,`orderNo`,`money`,`balance`,`time`,`IncDec`,`type`,`remark`,`payWay`) VALUES ('$user_id','$wb_order_id','$needPay_lin','$user_money_lin','$time2','0','1','微帮订单','$pay_way_lin')";
	            		//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
	            	$result=$db->query($sql);
	            	$user_money=$user_info[0]['userMoney']-$needPay_lin;
					$user_score=$user_info[0]['userScore']+$needPay_lin;
					$sql="update ".$oto."_users set userMoney='{$user_money}' where userId='{$user_id}'";
					$result=$db->query($sql);
					if($result)
					{
						$data[0]['pan']="1";
						$wb_id=$order[0]['helpId'];
						$acceptUserId=$order[0]['acceptUserId'];
						$sql="INSERT INTO ".$oto."_help_message (`helpId`,`userId`,`msg_type`,`createTime`) VALUES ('$wb_id','$acceptUserId','1','$time')";
		    			$result=$db->query($sql);
					}
					else
					{
						$sql="update ".$oto."_help_orders set orderStatus='0' where id='{$wb_order_id}'";
						$result=$db->query($sql);
						$data[0]['pan']=-1;
						$data[0]['msg']='支付异常';
					}
				}
				else
				{
					$data[0]['pan']="-1";
					$data[0]['msg']="账户余额不足";
				}
			}
			else if($pay_way==2 || $pay_way==1)
			{
				$needPay_lin=$order[0]['money'];
					$user_score_lin=$user_info[0]['userScore']+$needPay_lin;
					$user_money_lin=$user_info[0]['userMoney'];
					$sql="update ".$oto."_help_orders set orderStatus='1',payType='{$pay_way}',payTime='{$time}',isPay='1' where id='{$wb_order_id}'";
					$result=$db->query($sql);
					$sql="INSERT INTO ".$oto."_score_record (`userid`,`orderNo`,`score`,`totalscore`,`time`,`IncDec`,`type`) VALUES ('$user_id','$wb_order_id','$needPay_lin','$user_score_lin','$time2','1','1')";
	            	$result=$db->query($sql);
	            	if($pay_way==3) $pay_way_lin=3;
	            	if($pay_way==1) $pay_way_lin=1;
	            	if($pay_way==2) $pay_way_lin=2;
	            	$sql="INSERT INTO ".$oto."_money_record (`userid`,`orderNo`,`money`,`balance`,`time`,`IncDec`,`type`,`remark`,`payWay`) VALUES ('$user_id','$wb_order_id','$needPay_lin','$user_money_lin','$time2','0','1','微帮订单','$pay_way_lin')";
	            		//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
	            	$result=$db->query($sql);
	            	$user_money=$user_info[0]['userMoney'];
					$user_score=$user_info[0]['userScore']+$needPay_lin;
					$sql="update ".$oto."_users set userScore='{$user_score}' where userId='{$user_id}'";
					$result=$db->query($sql);
						$data[0]['pan']="1";
						$wb_id=$order[0]['helpId'];
						$acceptUserId=$order[0]['acceptUserId'];
						$sql="INSERT INTO ".$oto."_help_message (`helpId`,`userId`,`msg_type`,`createTime`) VALUES ('$wb_id','$acceptUserId','1','$time')";
		    			$result=$db->query($sql);
			}
		}
		echo json_encode($data);
?>